package com.imooc.bilibili.controller;

import com.imooc.bilibili.entity.BarrageInfo;
import com.imooc.bilibili.exception.ConditionException;
import com.imooc.bilibili.result.Result;
import com.imooc.bilibili.service.BarrageService;
import com.imooc.bilibili.support.UserSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/video-barrage")
public class BarrageController {

    @Autowired
    private BarrageService barrageService;

    @Autowired
    private UserSupport userSupport;

    @GetMapping("/query-video-barrage")
    public Result<List<BarrageInfo>> queryVideoBarrage(@RequestParam Long videoId,
                                                       String startTime,
                                                       String endTime) throws Exception {
        List<BarrageInfo> barrageInfos = new ArrayList<>();
        try {
            //判断当前是否为用户模式还是旅客模式
            //用户模式的userId不为null，旅客模式的用户id为null
            Long userId = userSupport.getCurrnetUserId().get("userId");
            //如果是用户模式，允许按照时间筛选
            barrageInfos = barrageService.getBarrageInfoService(videoId, startTime, endTime);

        } catch (Exception exception) {
            barrageInfos = barrageService.getBarrageInfoService(videoId, null, null);
        }
        return new Result<>(barrageInfos);
    }

}
